package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.service.PointService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.*;

/**
 * @author 王亚齐
 * @version v1.0
 * @package com.itheima.controller
 * @description 会员数量报表指定时间段展示
 * @date 2019/7/5 16:45
 **/
@RestController
@RequestMapping("/point")
public class PointController {
    @Reference
    private PointService pointService;

    /**
     * 会员数量报表指定时间段展示
     * @param date
     * @return
     */
    @RequestMapping("/getMemberPoint")
    public Result getMemberPoint(@RequestParam("value")String date) throws ParseException {

        SimpleDateFormat sdf = new SimpleDateFormat("E MMM dd yyyy HH:mm:ss z", Locale.US);
        Date logDate1 = sdf.parse(date);
        Instant instant = logDate1.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDate oldDate = instant.atZone(zoneId).toLocalDate();
        int oldDateYear = oldDate.getYear();
        int oldDateMonthValue = oldDate.getMonthValue();
        LocalDate nowDate = LocalDate.now();
        int nowDateYear = nowDate.getYear();
        int nowDateMonthValue = nowDate.getMonthValue();
        int diff = (nowDateYear - oldDateYear) * 12 + nowDateMonthValue - oldDateMonthValue;
        List<String> months = new ArrayList<>();
        for (int i = 1; i <= diff; i++) {
            LocalDate localDate = oldDate.plusMonths(i);
            System.out.println("localDate = " + localDate);

            months.add(localDate.toString().substring(0,7));
        }
        Map<String, Object> map = new HashMap<>();
        map.put("months", months);
        List<Integer> memberCount = pointService.findCountByMonths(months);
        map.put("memberCount", memberCount);
        return new Result(true, MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS, map);



    }

  /*  public static void main(String[] args) throws ParseException {
        String date = "Fri Jun 01 2018 00:00:00 GMT 0800 (中国标准时间)";
        SimpleDateFormat sdf = new SimpleDateFormat("E MMM dd yyyy HH:mm:ss z", Locale.US);
        Date logDate1 = sdf.parse(date);
        Instant instant = logDate1.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDate oldDate = instant.atZone(zoneId).toLocalDate();
        int oldDateYear = oldDate.getYear();
        int oldDateMonthValue = oldDate.getMonthValue();
        System.out.println("parse = " + oldDateYear);
        System.out.println("parse = " + oldDateMonthValue);
        System.out.println("oldDate = " + oldDate);
    }*/
}